//
// Created by mi on 2024/2/9.
//

#include <iostream>
#include <nlohmann/json.hpp>

using namespace std;
using namespace nlohmann;

namespace ns {
    struct person {
        std::string name;
        std::string address;
        int age;
        std::string code;
    };

    void to_json(json &j, const person &p) {
        j = json{{"name",    p.name},
                 {"address", p.address},
                 {"age",     p.age},
                 {"code",    p.code}
        };
    }
    void from_json(const json &j, person &p) {
        j.at("name").get_to(p.name);
        j.at("address").get_to(p.address);
        j.at("age").get_to(p.age);
    }
} // namespace ns

int main() {
    ns::person p{"Ned Flanders", "744 Evergreen Terrace", 60};
    json j = p;
    std::cout << j << std::endl;
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}

// conversion: json -> person
    auto p2 = j.get<ns::person>();
    return 0;
}